package com.tuniu.agents.impl.evaluator.impl;

import java.util.List;
import java.util.Map;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatResponse;

import com.tuniu.agents.advisor.AgentChatMemoryAdvisor;
import com.tuniu.agents.agent.AgentManagerGroup;
import com.tuniu.agents.agent.AgentOptions;
import com.tuniu.agents.impl.evaluator.EvaluatorAgent;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;




/**
 * Shopping Cart Evaluator Agent
 * Specifically used to evaluate shopping cart related metrics
 */
@Slf4j
public class CartEvaluatorAgent extends EvaluatorAgent {
    
        /**
         * Resources to be packaged
         */
        private static final String PACKAGE_RESOURCE = "package_resource";

        /**
         * Selected resources
         */
        private static final String SELECTED_RESOURCE = "selected_resource";

        /**
         * Fixed input
         */
        private static final String QUERY_STRING = "Help me evaluate the rationality of the plan";

    public CartEvaluatorAgent(AgentManagerGroup agentManagerGroup,
                            String agentId, String promptTemplateId) {
        super(agentManagerGroup,new AgentOptions(agentId, promptTemplateId));
    
    }

    /**
     * Shopping Cart Evaluator Agent Response
     * @param conversationId conversation ID
     * @param message user message
     * @return response
     */
    @Override
    public ChatResponse callResponse(String conversationId, List<Message> messages, Map<String, Object> context) {
        String message = messages.get(0).getText();
        //Generate a random number using current timestamp
        
        if(message.contains("|")){
            String[] messageArray = message.split("\\|");
            String selectedRes = messageArray[0];
            String packageRes = messageArray[1];
            return chatClient.prompt()
                .system(s -> s.param(PACKAGE_RESOURCE, packageRes).
                param(SELECTED_RESOURCE, selectedRes)) // Set package resources and selected resources in the prompt
                    .user(QUERY_STRING) //Fixed format input
                    .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId).
                    param(AgentChatMemoryAdvisor.CHAT_MEMORY_AGENT_ID_KEY, this.agentId))
                    .call()
                    .chatResponse();
       
        }
        return null;
    }

    /**
     * Shopping Cart Evaluator Agent Streaming Response
     * @param conversationId conversation ID
     * @param message user message
     * @return streaming response
     */
    @Override
    public Flux<ChatResponse> streamResponse(String conversationId, List<Message> messages, Map<String, Object> context) {
        String message = messages.get(0).getText();
        if(message.contains("|")){
          String[] messageArray = message.split("\\|");
          String selectedRes = messageArray[0];
          String packageRes = messageArray[1];
          return chatClient.prompt()
                  .system(s -> s.param(PACKAGE_RESOURCE, "").
                  param(SELECTED_RESOURCE, "")) // Set current session time for model recognition and set package and selected resources
                  .user(message)
                  .stream()
                  .chatResponse();
        }
        return null;
    }

   

} 